import * as THREE from "three"
import { CONFIG } from "../config.js"
import { OBJECTS, ID, PLAYER } from "../declarations.js"
import { INPUT } from "../input.js"
import { GAME } from "./game"
import { RAYCAST } from "./raycast"
import { MATERIAL } from "./material"
import  { DRIVE } from '../drive'


let delta = 0
OBJECTS.clock = new THREE.Clock()

let RENDER = {
  start: () => {
    RENDER.drawFrame()
  },

  drawFrame: () => {
    OBJECTS.delta += OBJECTS.clock.getDelta()

    if (OBJECTS.camera.controls) OBJECTS.camera.controls.update()

    if (CONFIG.postprocessing.enabled == true) {
      if (OBJECTS.postprocessing.composer) {
        OBJECTS.postprocessing.composer.render()
      }
    } else {
      OBJECTS.renderer.clear()
      OBJECTS.renderer.clearDepth()
      OBJECTS.renderer.render(OBJECTS.scenes.mainScene, OBJECTS.currentCamera)
    }

    GAME.tick()
    INPUT.tick()
    RAYCAST.tick()
    INPUT.tick()
    MATERIAL.tick()
    DRIVE.tick()
    ID.renderId = window.requestAnimationFrame(RENDER.drawFrame)
    OBJECTS.delta = OBJECTS.delta % OBJECTS.interval
  },
}

export { RENDER }
